<script setup lang="ts">
import { useConversationStore } from '@/stores/conversation'
import NewConversation from '../conversation/NewConversation.vue'
import ConversationContainer from '../conversation/index.vue'
import { useAppStore } from '@/stores/app'
import { useChatbotStore } from '@/stores/chatbot'
import { onBeforeMount, watch } from 'vue'
import Swal from 'sweetalert2'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const props = defineProps<{
  token: string
}>()

const router = useRouter()

const appStore = useAppStore()
const conversationStore = useConversationStore()
const chatbotStore = useChatbotStore()
const userStore = useUserStore()

onBeforeMount(() => {
  // 校验token
  if (!props.token) {
    Swal.fire({
      text: '参数不正确！',
      icon: 'warning',
      confirmButtonText: '确认',
      confirmButtonColor: '#3085d6',
    }).then(() => {
      router.replace('/404')
    })
    return
  } else {
    const token = props.token
    try {
      const userName = atob(token)
      if (userName !== userStore.userName) {
        conversationStore.currentKey = ''
        conversationStore.conversationList = []
        userStore.setUsername(userName)
      } else {
        conversationStore.conversationList = chatbotStore.getLocalConversationList()
        if (conversationStore.conversationList.length) {
          conversationStore.conversationList[0].key = conversationStore.conversationList[0]
            .id as string
          conversationStore.currentKey = conversationStore.conversationList[0].key
        }
      }
      userStore.setUsername(userName)
    } catch {
      Swal.fire({
        text: '参数不正确！',
        icon: 'warning',
        confirmButtonText: '确认',
        confirmButtonColor: '#3085d6',
      }).then(() => {
        router.replace('/404')
      })
      return
    }
  }
})

watch(
  () => conversationStore.conversationList,
  (newVal) => {
    chatbotStore.setLocalConversationList(newVal)
  },
  { deep: true },
)

const handleAddNewConversation = (inputs: object) => {
  const time = Date.now()
  const key = String(time)
  const conversation = {
    name: '新对话',
    key,
    introduction: appStore.openingStatement,
    created_at: time,
    updated_at: time,
    inputs,
  }
  conversationStore.addConversation(conversation)
  chatbotStore.setLocalConversationList([conversation])
  conversationStore.currentKey = key
}

const handleRestartConversation = async () => {
  const result = await Swal.fire({
    text: '是否确认重新开始对话？',
    icon: 'warning',
    showCancelButton: true,
    confirmButtonText: '确认',
    confirmButtonColor: '#3085d6',
    cancelButtonText: '取消',
    cancelButtonColor: '#f6f6f6',
    reverseButtons: true,
  })
  if (result.isConfirmed) {
    conversationStore.currentKey = ''
    conversationStore.conversationList = []
  }
}
</script>

<template>
  <NewConversation
    v-if="!conversationStore.currentKey"
    no-toggle
    @add-conversation="handleAddNewConversation"
  />
  <ConversationContainer v-else-if="!conversationStore.loading" no-toggle>
    <v-btn icon="mdi-restart" @click="handleRestartConversation"></v-btn>
  </ConversationContainer>
</template>
